<template>
  <div class="art-pub-container">
    <el-form
      :model="pubForm"
      :rules="pubRules"
      ref="pubRef"
      label-width="100px"
    >
      <el-form-item label="文章标题" placeholder="请填写文章标题" prop="title">
        <el-input v-model="pubForm.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select v-model="pubForm.cate_id" placeholder="请选择分类" clearable>
          <el-option
            v-for="item in cateList"
            :key="item.id"
            :label="item.cate_name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容">
        <!-- 富文本编辑器 -->
        <quill-editor v-model="pubForm.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面">
        <div class="cover-box">
          <img :src="pubForm.cover_img" alt="" class="cover-img" />
          <el-link
            type="primary"
            :underline="false"
            @click="$refs.iptFile.click()"
            >+ 选择封面</el-link
          >
          <!-- accept="image/*" 限制只允许上传图片 非图片文件选择弹框中不显示 -->
          <input
            type="file"
            ref="iptFile"
            accept="image/*"
            hidden
            @change="uploadImg"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="publish('已发布')"
          >发布文章</el-button
        >
        <el-button type="info" @click="publish('草稿')">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reqPublishArticle } from '@/api/article'
import imgUrl from '@/assets/images/cover.jpg'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      file: null,
      // 发布文章form对象
      pubForm: {
        title: '', // 文章标题
        cate_id: '', // 文章分类
        content: '', // 文章内容
        state: '', // 文章状态
        cover_img: imgUrl // 文章封面
      },
      // 发布文章的title和分类的校验规则
      pubRules: {
        title: [
          {
            required: true,
            message: '文章标题不能为空',
            trigger: ['change', 'blur']
          },
          {
            min: 1,
            max: 30,
            message: '文章标题1-30字符之间',
            trigger: ['change', 'blur']
          }
        ],
        cate_id: [
          {
            required: true,
            message: '请选择所属分类',
            trigger: ['change', 'blur']
          }
        ]
      }
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  methods: {
    // 上传图像本地预览
    uploadImg (e) {
      const file = e.target.files[0]
      if (file) {
        //  blob格式
        const tmp = URL.createObjectURL(file)
        this.pubForm.cover_img = tmp
        // 存一个file对象, 后期发布文章使用
        this.file = file
      }
    },
    publish (str) {
      // 处理state状态字段
      this.pubForm.state = str
      console.log(this.pubForm)
      // 1. 预校验
      this.$refs.pubRef.validate(async boo => {
        console.log(boo, 99)
        if (!boo) return ''
        // 2. 处理数据格式为formData格式
        const fm = new FormData()
        fm.append('title', this.pubForm.title)
        fm.append('cate_id', this.pubForm.cate_id)
        fm.append('content', this.pubForm.content)
        fm.append('state', this.pubForm.state)
        // cover_img要的是一个文件对象
        fm.append('cover_img', this.file)
        // 2. 发送发表文章接口请求
        const { data } = await reqPublishArticle(fm)
        // 3. 失败提示
        if (data.code !== 0) return this.$message.error(data.message)
        // 4.成功提示
        this.$message.success(data.message)
        // 5. 子传父通知父组件 关闭对话框
        this.$emit('addClose')
      })
    }
  }
}
</script>


<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>
